package com.vecspace.datacenter.datatransform.service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Map;
import java.util.Map.Entry;

import com.vecspace.database.model.ConnectionParam;
import com.vecspace.httpclient.THttpClient;
import com.vecspace.utils.log.HLogger;
/**
 * 
 * @author: wanghua
 */
public abstract class Soap2DatabaseETL extends DataETL{

	@Override
	public boolean execute(String args) {
		Connection conn = null;		
		try{
			conn = this.getConnection();
			return doDataETL(conn, args);
		}catch(Exception ex){
			HLogger.DEFAULT().error(ex);
			return false;
		}
		finally{
			try {
				if(conn != null){
					conn.close();
					conn = null;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**执行ETL作业
	 * 建议使用DbUtils工具类进行数据库操作。
	 * @param conn
	 * @param args
	 * @return
	 * @throws Exception
	 * @author: wanghua
	 */
	public abstract boolean doDataETL(Connection conn, String args) throws Exception;
	
	/**发起webservice请求，返回xml结果
	 * params参数会替换soapRequestData中的变量
	 * @param params
	 * @return
	 * @throws Exception 
	 * @author: wanghua
	 */
	protected String getSoapData(Map<String, String> params) throws Exception{
		String soapRequestData = this.soapRequestData;
		if(params != null)
		{
			for(Entry<String, String> entry : params.entrySet()){
				String pname = "{"+entry.getKey()+"}";
				String pvalue = entry.getValue();
				soapRequestData = soapRequestData.replace(pname, pvalue);
			}
		}
		HLogger.DEFAULT().info(soapRequestData);
		String soapResponseData = THttpClient.postWSSoap(wsurl, soapRequestData, this.getMimeType(), this.getCharset());
		return soapResponseData;
	}
	
	//定义请求参数 参数中带参数
	
	/**
	 * 
	 * 功能描述：
	 * @return
	 * @throws Exception 
	 * @author: wanghua
	 */
	protected Connection getConnection() throws Exception{
		ConnectionParam connparam = connParam;
		Class.forName(connparam.getDriverClassName());
		Connection conn = DriverManager.getConnection(connparam.getUrl(),connparam.getUserId(),connparam.getPassword());
		return conn;
	}
	
	/**
	 * @param connparam
	 * @return
	 * @throws Exception 
	 * @author: wanghua
	 */
	protected Connection getConnection(ConnectionParam connparam) throws Exception{
		if(connparam == null)return null;
		Class.forName(connparam.getDriverClassName());
		Connection conn = DriverManager.getConnection(connparam.getUrl(),connparam.getUserId(),connparam.getPassword());
		return conn;
	}
	
	private ConnectionParam connParam;
	private String wsurl;
	private String soapRequestData;
	private String mimeType = "text/xml";
	private String charset = "utf-8";
	
	public ConnectionParam getConnParam() {
		return connParam;
	}

	public void setConnParam(ConnectionParam connParam) {
		this.connParam = connParam;
	}

	public String getWsurl() {
		return wsurl;
	}

	public void setWsurl(String wsurl) {
		this.wsurl = wsurl;
	}

	public String getSoapRequestData() {
		return soapRequestData;
	}

	public void setSoapRequestData(String soapRequestData) {
		this.soapRequestData = soapRequestData;
	}

	public String getCharset() {
		return charset;
	}

	public void setCharset(String charset) {
		this.charset = charset;
	}

	public String getMimeType() {
		return mimeType;
	}

	public void setMimeType(String mimeType) {
		this.mimeType = mimeType;
	}
	
}
